package com.ruoyi.asms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.asms.domain.AsmsFaultDispatchStage;
import com.ruoyi.asms.service.IAsmsFaultDispatchStageService;
import com.ruoyi.asms.service.IFaultInfoViewService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 故障派单阶段信息Controller
 * 
 * @author yfkj
 * @date 2021-10-21
 */
@RestController
@RequestMapping("/asms/AsmsFaultDispatchStage")
public class AsmsFaultDispatchStageController extends BaseController
{
    @Autowired
    private IAsmsFaultDispatchStageService asmsFaultDispatchStageService;
    @Autowired
    private IFaultInfoViewService faultInfoViewService;
    /**
     * 查询故障派单阶段信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultDispatchStage:list")
    @GetMapping("/list")
    public TableDataInfo list(AsmsFaultDispatchStage asmsFaultDispatchStage)
    {
        startPage();
        List<AsmsFaultDispatchStage> list = asmsFaultDispatchStageService.selectAsmsFaultDispatchStageList(asmsFaultDispatchStage);
        return getDataTable(list);
    }

    /**
     * 导出故障派单阶段信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultDispatchStage:export")
    @Log(title = "故障派单阶段信息", businessType = BusinessType.EXPORT)
    @PostMapping(value="/export", produces = "application/octet-stream")
    public void export(HttpServletResponse response, AsmsFaultDispatchStage asmsFaultDispatchStage) throws IOException
    {
        List<AsmsFaultDispatchStage> list = asmsFaultDispatchStageService.selectAsmsFaultDispatchStageList(asmsFaultDispatchStage);
        ExcelUtil<AsmsFaultDispatchStage> util = new ExcelUtil<AsmsFaultDispatchStage>(AsmsFaultDispatchStage.class);
        util.exportExcel(response, list, "故障派单阶段信息数据");
    }

    /**
     * 获取故障派单阶段信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultDispatchStage:query")
    @GetMapping(value = "/{faultDispatchStageId}")
    public AjaxResult getInfo(@PathVariable("faultDispatchStageId") Long faultDispatchStageId)
    {
        return AjaxResult.success(asmsFaultDispatchStageService.selectAsmsFaultDispatchStageByFaultDispatchStageId(faultDispatchStageId));
    }

    /**
     * 新增故障派单阶段信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultDispatchStage:add")
    @Log(title = "故障派单阶段信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AsmsFaultDispatchStage asmsFaultDispatchStage)
    {
        return toAjax(asmsFaultDispatchStageService.insertAsmsFaultDispatchStage(asmsFaultDispatchStage));
    }

    /**
     * 修改故障派单阶段信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultDispatchStage:edit")
    @Log(title = "故障派单阶段信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AsmsFaultDispatchStage asmsFaultDispatchStage)
    {
        return toAjax(asmsFaultDispatchStageService.updateAsmsFaultDispatchStage(asmsFaultDispatchStage));
    }

    /**
     * 删除故障派单阶段信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:AsmsFaultDispatchStage:remove")
    @Log(title = "故障派单阶段信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{faultDispatchStageIds}")
    public AjaxResult remove(@PathVariable Long[] faultDispatchStageIds)
    {
        return toAjax(asmsFaultDispatchStageService.deleteAsmsFaultDispatchStageByFaultDispatchStageIds(faultDispatchStageIds));
    }

    /**
     * 定时任务检查未处理故障
     * @return
     */
    @GetMapping("/checkOvertimeDispatches")
    public R<Map<Long,Integer>> checkOvertimeDispatches(@RequestParam("overTime")Long overTime){
        //List<FaultInfoView> faultInfoViews =faultInfoViewService.list(new QueryWrapper<FaultInfoView>().eq(FaultInfoView.STAGE,2).eq(FaultInfoView.IS_FINISH,1));
        Map<Long,Integer> resultMap =new HashMap<>();
        List<AsmsFaultDispatchStage> list = asmsFaultDispatchStageService.list(new QueryWrapper<AsmsFaultDispatchStage>().eq(AsmsFaultDispatchStage.STATUS, 0).lt(AsmsFaultDispatchStage.START_TIME, System.currentTimeMillis() - overTime));
        if(list.size()>0){
            list.forEach(item-> resultMap.merge(item.getMaintenanceDeptId(), 1, (a, b) -> a + b));
        }
        return R.ok(resultMap);

    }

}
